/*****************************************************
Copyright (C) 2020. All rights reserved.
File name     ：TestMain.c
Version       ：v1.0
Author        ：zhengqijun
Date          ：2020-03-29
Function List ：
Description   ：main function
******************************************************/

#include <stdio.h>  // for printf
#include "SplayTree.h"

// PrintTree for testing
static void PrintDepth(ElementType X, int depth);
static void PrintTree(SplayTree T, int depth);

void PrintDepth(ElementType X, int depth) {
    while (depth != 0) {
        printf("    ");
        --depth;
    }
    printf("%d\n", X);
}

void PrintTree(SplayTree T, int depth) {
    if (IsEmpty(T)) {
        printf("SplayTree is empty!\n");
        return;
    }

    if (!IsEmpty(Right(T))) {
        PrintTree(Right(T), depth + 1);
    }

    PrintDepth(Retrieve(T), depth);

    if (!IsEmpty(Left(T))) {
        PrintTree(Left(T), depth + 1);
    }
}

// main function
int main(void) {
    SplayTree T = NULL;

    T = Initialize();
    T = MakeEmpty(T);

    if (IsEmpty(T)) {
        printf("SplayTree is empty!\n");
    } else {
        printf("SplayTree is not empty!\n");
    }

    {  /* Insert */
        T = Insert(1, T);
        T = Insert(2, T);
        T = Insert(3, T);
        T = Insert(4, T);
        T = Insert(5, T);
        T = Insert(6, T);
        T = Insert(7, T);
    }

    printf("Print inserted splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    if (IsEmpty(T)) {
        printf("SplayTree is empty!\n");
    } else {
        printf("SplayTree is not empty!\n");
    }

    printf("The preorder traversal of splay tree is: [ ");
    PreorderTraversal(T);
    printf("]\n");

    printf("The inorder traversal of splay tree is: [ ");
    InorderTraversal(T);
    printf("]\n");

    printf("The postorder traversal of splay tree is: [ ");
    PostorderTraversal(T);
    printf("]\n");

    printf("The levelorder traversal of splay tree is: [ ");
    LevelorderTraversal(T);
    printf("]\n");

    T = Find(1, T);
    if (Retrieve(T) == 1) {
        printf("Find element 1 in splay tree!\n");
    } else {
        printf("Can not find element 1 in splay tree!\n");
    }

    printf("Print splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    T = Find(9, T);
    if (Retrieve(T) == 9) {
        printf("Find element 9 in splay tree!\n");
    } else {
        printf("Can not find element 9 in splay tree!\n");
    }

    printf("Print splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    printf("Min is %d in splay tree!\n", Retrieve(T = FindMin(T)));
    printf("Print splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    printf("Max is %d in splay tree!\n", Retrieve(T = FindMax(T)));
    printf("Print splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    T = Remove(4, T);
    printf("Remove element 4 in splay tree!\n");
    printf("Print splay tree:\n");
    PrintTree(T, 0);
    printf("\n");

    T = MakeEmpty(T);

    return 0;
}

// EOF

